//
//  YFMineCellViewModel.m
//  project
//
//  Created by yefei on 2019/5/6.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMineCellViewModel.h"
#import "YFDongtaiModel.h"

@implementation YFMineCellViewModel

- (instancetype)initWithModel:(YFDongtaiRecordModel *)model {
    self = [super init];
    if (self) {
        self.model = model;
        [self setupPropertiesWithModel:model];
    }
    return self;
}

- (void)setupPropertiesWithModel:(YFDongtaiRecordModel *)model {
    
    self.isImage = model.isImage;
    self.praiseAmount = model.praiseAmount;
    self.isPraise = model.isPraise.boolValue;
    self.albumId = model.albumId;
    // title
    self.title = model.content;
    self.likeAmount  = model.likeAmount;
    self.time = model.time;
    self.reviewAmount = model.reviewAmount;
    
    // 是图片
    if (model.isImage) {
        [self setupImageView:model];
        
        NSMutableArray<NSString *> *imageURLs = [NSMutableArray arrayWithCapacity:model.files.count];
        for (YFDongtaiFileModel *file in model.files) {
            [imageURLs addObject:file.url];
        }
        self.imageURLs = imageURLs;
        
    } else {
        // 是视频
        [self setupVideoWithModel:model];
        self.thumbnail = model.files.firstObject.thumbnail;
        if (model.files.count == 1) {
            self.videoURL = model.files.firstObject.url;
        }
    }
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //调整行间距
    paragraphStyle.lineSpacing = 2;
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,
                                NSKernAttributeName:@(1),
                                NSFontAttributeName:[UIFont fontWithName:PingFangSC_Medium size:14]};
    
    NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc] initWithString:model.content attributes:attriDict];
    
    CGSize titleSize = [attributeText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil].size;
    
    // 计算title的高度
//    CGSize titleSize = [model.content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT)
//                                                   options:(NSStringDrawingUsesLineFragmentOrigin)
//                                                attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
//                                                   context:nil].size;
    self.titleHeight = titleSize.height;
    if (self.titleHeight < 21) {
        self.titleHeight = 21;
    }
    
    self.attributeTitle = attributeText;
    
    // 高度
    self.cellHeight = 10 + 5 + 50 + self.content.bounds.size.height + self.titleHeight;
}


#pragma mark - 设置图片
- (void)setupImageView:(YFDongtaiRecordModel *)model {
    
    [self createImageViewWithModel:model];
    
    NSInteger index = 0;
    
    for (int i = 0; i < model.files.count; i++) {
        
        // 获得对应索引的 imageView
        UIView *v = self.content.subviews[index];
        
        UIImageView *iv;
        
        if ([v isKindOfClass:UIImageView.class]) {
            iv = (UIImageView *)v;
        }
        iv.tag = i;
        
        // 如果是 4 张图， 两排显示
        if (index == 1 && model.files.count == 4) {
            index += 1;
        }
        [iv sd_setImageWithURL:[NSURL URLWithString:model.files[i].url] placeholderImage:nil];
        iv.hidden = NO;
        index += 1;
    }
}

#pragma mark - 创建图片
- (void)createImageViewWithModel:(YFDongtaiRecordModel *)model  {
    
    UIView *imageContentView = [[UIView alloc] init];
    
    if (model.files.count == 1) {
        imageContentView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 40, (SCREEN_WIDTH - 40) * 182 / 343);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageContentView.bounds];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.files[0].thumbnail] placeholderImage:nil];
        imageView.userInteractionEnabled = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [imageView rounded:(UIRectCornerAllCorners) radius:5];
        
        [imageContentView addSubview:imageView];
        self.content = imageContentView;
        return;
    }
    
    CGFloat imageContentHeight = 0;
    // 3列
    NSInteger count = 3;
    
    NSInteger ivCount = model.files.count;
    
    if (ivCount == 4) {
        ivCount = 5;
    }
    // 图片之间的间距
    CGFloat margin = 10;
    
    // 宽
    CGFloat width = (SCREEN_WIDTH - 20 - 30 - margin * 2) / count;
    
    // 高
    CGFloat height = width;
    
    if (ivCount <= 3) {
        imageContentHeight = height;
    } else if (ivCount > 3 && ivCount <= 6){
        imageContentHeight = height * 2 + margin;
    } else {
        imageContentHeight = height * 3 + margin * 2;
    }
    imageContentView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 20 - 30, imageContentHeight);
    
    for (int i = 0; i < ivCount; i ++) {
        UIImageView *iv = UIImageView.new;
        iv.userInteractionEnabled = YES;
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        iv.tag = i;
        iv.hidden = YES;
        
        // 行
        CGFloat row = i / count;
        // 列
        CGFloat col = i % count;
        
        iv.frame = CGRectMake(col * (width + margin), row * (height + margin), width, height);
        [iv rounded:(UIRectCornerAllCorners) radius:5];
        [imageContentView addSubview:iv];
    }
    self.content = imageContentView;
}

#pragma mark - 设置视频
- (void)setupVideoWithModel:(YFDongtaiRecordModel *)model {
    
    UIView *videoContentView = [[UIView alloc] init];
    videoContentView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 40, (SCREEN_WIDTH - 40) * 182 / 343);
    
    UIImageView *coverImageView = [[UIImageView alloc] initWithFrame:videoContentView.bounds];
    [coverImageView sd_setImageWithURL:[NSURL URLWithString:model.files[0].thumbnail] placeholderImage:nil];
    coverImageView.userInteractionEnabled = YES;
    coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    [coverImageView rounded:(UIRectCornerAllCorners) radius:5];
    
    UIButton *playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playButton.frame = CGRectMake(0, 0, 70, 70);
    playButton.center = coverImageView.center;
    [playButton setImage:[UIImage imageNamed:@"icon_myDongtai_play"] forState:(UIControlStateNormal)];
    [videoContentView addSubview:coverImageView];
    [coverImageView addSubview:playButton];
    self.content = videoContentView;
}


@end
